import 'package:bmprogresshud/bmprogresshud.dart';
import 'package:car_baidu/core/service/mine_request.dart';
import 'package:car_baidu/ui/shared/theme/app_theme.dart';
import 'package:car_baidu/ui/widget/single_input.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../../shared/px/int_extentsion.dart';

class LLModifyPasswordContent extends StatefulWidget {
  const LLModifyPasswordContent({Key? key}) : super(key: key);

  @override
  _LLModifyPasswordContentState createState() =>
      _LLModifyPasswordContentState();
}

class _LLModifyPasswordContentState extends State<LLModifyPasswordContent> {
  final TextEditingController passwordController = TextEditingController();
  final FocusNode passwordFocusNode = FocusNode();
  final TextEditingController newPasswordController = TextEditingController();
  final FocusNode newPasswordFocusNode = FocusNode();
  final TextEditingController againPasswordController = TextEditingController();
  final FocusNode againPasswordFocusNode = FocusNode();
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        SizedBox(
          height: 2.px,
        ),
        Container(
          alignment: Alignment.center,
          padding: EdgeInsets.symmetric(horizontal: 11.px),
          color: Colors.white,
          height: 44.px,
          child: LLSingleInputWidget(
            controller: passwordController,
            focusNode: passwordFocusNode,
            leading: Container(
              width: 77.px,
              child: Text(
                "旧密码",
                style: LLAppTheme.largerTextStyle,
              ),
            ),
            placeHold: "请输入旧密码",
          ),
        ),
        SizedBox(
          height: 2.px,
        ),
        Container(
          alignment: Alignment.center,
          padding: EdgeInsets.symmetric(horizontal: 11.px),
          color: Colors.white,
          height: 44.px,
          child: LLSingleInputWidget(
            controller: newPasswordController,
            focusNode: newPasswordFocusNode,
            leading: Container(
              width: 77.px,
              child: Text(
                "新密码",
                style: LLAppTheme.largerTextStyle,
              ),
            ),
            placeHold: "请输入密码",
          ),
        ),
        SizedBox(
          height: 2.px,
        ),
        Container(
          alignment: Alignment.center,
          padding: EdgeInsets.symmetric(horizontal: 11.px),
          color: Colors.white,
          height: 44.px,
          child: LLSingleInputWidget(
            controller: againPasswordController,
            focusNode: againPasswordFocusNode,
            leading: Container(
              width: 77.px,
              child: Text(
                "确认密码",
                style: LLAppTheme.largerTextStyle,
              ),
            ),
            placeHold: "请再次输入密码",
          ),
        ),
        SizedBox(
          height: 40.px,
        ),
        Container(
          margin: EdgeInsets.symmetric(horizontal: 41.px),
          height: 43.px,
          decoration: BoxDecoration(
              color: LLAppTheme.primaryColor,
              borderRadius: BorderRadius.circular(3.px)),
          child: TextButton(
            onPressed: modifyPasswordRequest,
            child: Text(
              "确认修改",
              style: LLAppTheme.largerTextStyle.copyWith(color: Colors.white),
            ),
          ),
        )
      ],
    );
  }

  void modifyPasswordRequest() {
    passwordFocusNode.unfocus();
    newPasswordFocusNode.unfocus();
    againPasswordFocusNode.unfocus();
    InterceptorsWrapper wrapper = InterceptorsWrapper(onRequest: (req, handle) {
      ProgressHud.showLoading(text: "正在修改");
      handle.next(req);
    }, onResponse: (res, handle) {
      print(res.data);
      if (res.data["success"] == true) {
        ProgressHud.showSuccessAndDismiss(text: "修改成功");
      } else {
        ProgressHud.showErrorAndDismiss(text: "修改失败");
      }
      handle.resolve(res);
    }, onError: (err, handle) {
      ProgressHud.showErrorAndDismiss(text: "修改失败");
      handle.next(err);
    });
    LLMineRequest.modifyPasswordRequest(passwordController.text,
        newPasswordController.text, againPasswordController.text,
        wrapper: wrapper);
  }
}
